/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) 2013  <copyright holder> <email>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "LandmarkPattern.h"

LandmarkPattern::LandmarkPattern()
{
  createCircularPattern(24);
}

LandmarkPattern::~LandmarkPattern()
{

}

void LandmarkPattern::createCircularPattern(int radius)
{
  this->radius = radius;
  
  float lineWidth = radius / 10;
  float sigma = lineWidth / 6;
  int offset = lineWidth;
  int patternSize = 2 * radius + 1 + 2 * offset;
  pattern = Mat::zeros(patternSize, patternSize, CV_32FC1);
  patternV = Mat::zeros(patternSize, patternSize, CV_32FC1);
  patternH = Mat::zeros(patternSize, patternSize, CV_32FC1);
  
  for (int i = 0; i < patternSize; i++)
    for (int j = 0; j < patternSize; j++)
    {
      // x use i as cols, y use j as rows
      float x = i - radius - 1 - offset;
      float y = j - radius - 1 - offset;
      
      Mat dirVec = Mat::zeros(1, 2, CV_32FC1);
      dirVec.at<float>(0,0) = x;
      dirVec.at<float>(0,1) = y;
      
      
      double normDirVec = norm(dirVec, NORM_L2);
      
      float deltaR = (x * x + y * y - radius * radius) / (normDirVec + radius);
      pattern.at<float>(j,i) = exp(-(deltaR * deltaR)/(2 * sigma * sigma)) * 255;
      if (normDirVec != 0 )
      {
	patternV.at<float>(j,i) = pattern.at<float>(j,i) * y / normDirVec;
	patternH.at<float>(j,i) = pattern.at<float>(j,i) * x / normDirVec;
      } 
      else 
      {
	patternV.at<float>(j,i) = 0;
	patternH.at<float>(j,i) = 0;
      }
    }
  
}


Mat LandmarkPattern::getPatternMatrix()
{
  return pattern;
}

Mat LandmarkPattern::getPatternV()
{
  return patternV;
}

Mat LandmarkPattern::getPatternH()
{
  return patternH;
}

int LandmarkPattern::getRadius()
{
  return radius;
}